/**
 * org.vz.product.vserv.pf.impl
 * @since : jdk 1.8
 */
package org.vz.product.vserv.pf.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.beetl.sql.core.OnConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.vz.product.vserv.pf.dao.ReceivedSerParticularsDao;
import org.vz.product.vserv.pf.domain.ReceivedSerParticulars;
import org.vz.product.vserv.pf.domain.entity.RecivedServedSerParticulats;
import org.vz.product.vserv.pf.domain.result.ReceivedSerParticularsResult;
import org.vz.product.vserv.pf.service.ReceivedSerParticularsService;

/**
 * @author morik is Yongxu.Wang 获取时间段服务概述
 * @CreateTime 2017年12月06日 13:32:14
 * @version 1.0.0
 * @description TODO
 */
@Service
public class ReceivedSerParticularsServiceImpl implements
		ReceivedSerParticularsService {
	@Autowired
	ReceivedSerParticularsDao receivedSerParticularsDao;

	String sql = "[PF.Log].[dbo].Get_ServiceCounts_ByHOUR";;

	@Override
	public ReceivedSerParticularsResult receivedSerParticularsService(int type) {
		ReceivedSerParticularsResult rspr = new ReceivedSerParticularsResult();
		List<ReceivedSerParticulars> data = new ArrayList<ReceivedSerParticulars>();
		if (type == 1) {
			sql = "EXECUTE [PF.Log].[dbo].Get_ServiceCounts_ByDay";
		} else if (type == 2) {
			sql = "EXECUTE [PF.Log].[dbo].Get_ServiceCounts_ByHOUR";
		} else {
			sql = "EXECUTE [PF.Log].[dbo].Get_ServiceCounts_ByHOUR";
		}
		data = receivedSerParticularsDao.getSQLManager().executeOnConnection(
				new OnConnection<List<ReceivedSerParticulars>>() {
					@Override
					public List<ReceivedSerParticulars> call(Connection conn)
							throws SQLException {
						List<ReceivedSerParticulars> d = new ArrayList<ReceivedSerParticulars>();
						System.out.println("conn" + conn);
						CallableStatement ps = conn.prepareCall(sql);
						ps.execute();
						while ((ps.getMoreResults())
								|| (ps.getUpdateCount() != -1)) {
							if (ps.getUpdateCount() == -1) {
								ResultSet rs = ps.getResultSet();
								while (rs.next()) {
									ReceivedSerParticulars r = new ReceivedSerParticulars();
									r.setDuringTime(rs.getString("duringTime"));
									r.setExceptionCount(rs
											.getLong("exceptionCount"));
									r.setServiceName(rs
											.getString("serviceName"));
									r.setInterfaceId(rs
											.getString("interfaceId"));
									r.setUseCount(rs.getLong("useCount"));
									d.add(r);
								}
							}
						}
						return d;
					}
				});
		RecivedServedSerParticulats rs = new RecivedServedSerParticulats();
		int i = 0;
		int j = 0;
		for (int k = 0; k < data.size() / 6; k++) {
			if (i == 0) {
				rs = new RecivedServedSerParticulats();
			}
			for (; i < 6; i++) {
				j++;
				rs.useCounts[i] = data.get(j - 1).getUseCount();
				rs.exceptionCounts[i] = data.get(j - 1).getExceptionCount();
				rs.times[i] = data.get(j - 1).getDuringTime();
				if (i == 5) {
					rs.useCount = data.get(j - 1).getUseCount();
					rs.serciceId = data.get(j - 1).getInterfaceId();
					rs.serviceName = data.get(j - 1).getServiceName();
					i = 0;
					rspr.data.add(rs);
					break;
				}
			}
		}
		rspr.msg = "查询成功！！！";
		rspr.status = 1;
		return rspr;
	}
}
